package com.ming.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ming.domain.Basket;
import com.ming.sevice.BasketService;
import com.ming.vo.CartMoney;
import com.ming.vo.ShopCartResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:何元才
 * @Desc:
 * @data:2021-04-08 19:29
 */
@RestController
@RequestMapping("p/shopCart")
@Api(tags = "购物车管理接口")
public class BasketController {

    @Autowired
    private BasketService basketService;

    @GetMapping("prodCount")
    @ApiOperation("查询用户购物车总条数")
    public ResponseEntity<Integer> getBasketCount(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        Integer count = basketService.getBasketCountByUserId(userId);
        return ResponseEntity.ok(count);
    }

    @PostMapping("info")
    @ApiOperation("查询购物车详情")
    public ResponseEntity<List<ShopCartResult>> cartInfo(){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        List<ShopCartResult> shopCartResults = basketService.getCartInfo(userId);
        return ResponseEntity.ok(shopCartResults);
    }

    /**
     * 商品里面修改 可能是增加 也可能是修改
     *
     * @param basket
     * @return
     */
    @PostMapping("changeItem")
    @ApiOperation("改变购物车商品信息")
    public ResponseEntity<Void> changeItem(@RequestBody Basket basket){
        String userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        basket.setUserId(userId);
        basketService.changeItem(basket);
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("deleteItem")
    @ApiOperation("删除购物车选中的商品")
    public ResponseEntity<Void> cartInfo(@RequestBody List<Long> basketIds) {
        basketService.removeByIds(basketIds);
        return ResponseEntity.ok().build();
    }

    /**
     * 返回一个对象
     * 最终金额
     * 实际金额
     * 优惠了多少钱
     * 运费
     * @param basketIds
     * @return
     */
    @PostMapping("totalPay")
    @ApiOperation("计算购物车选中的商品总金额")
    public ResponseEntity<CartMoney> totalPay(@RequestBody List<Long> basketIds){
        CartMoney cartMoney = basketService.calcCartMoney(basketIds);
        return ResponseEntity.ok(cartMoney);
    }


    /**
     * 提供远程调用购物车 根据ids拿到集合
     *
     * @param basketIds
     * @return
     */
    @PostMapping("getCartsByIds")
    List<Basket> getCartsByIds(@RequestBody List<Long> basketIds){
        return basketService.listByIds(basketIds);
    }

    /**
     * 提供远程调用清空购物车的方法
     *
     * @param userId
     * @param skuIds
     */
    @PostMapping("clearCart")
    @ApiOperation("提供远程调用清空购物车的方法")
    void clearCart(@RequestParam String userId, @RequestBody List<Long> skuIds) {
        basketService.remove(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getUserId, userId)
                .in(Basket::getSkuId, skuIds)
        );
    }


}
